package 链表;

public class LinkedList<E> {
    private class Node{
        public Node next;
        public E e ;
        public Node(Node next, E e) {
            this.next = next;
            this.e = e;
        }
        public Node(E e ) {
            this(null, e);
        }
        public Node() {
            this(null, null);
        }
        @Override
        public String toString() {
            return e.toString();
        }
    }
    private Node head;
    private int size;

    public LinkedList() {
        head = new Node(null, null);
        size = 0;
    }
    /*获取个数*/
    public int getSize(){
        return size;
    }
    /*判空*/
    public boolean isEmpty(){
        return size == 0;
    }
    /*头部添加*/
    public void addFirst(E e){
       add(e, 0);
    }
    /*中间索引i添加*/
    public void add(E e, int index){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("error");
        }
        Node pre = head;
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }
        Node node = new Node(e);
        node.next = pre.next;
        pre.next = node;
        size++;
    }
    /*尾部增加*/
    public void addLast(E e){
        add(e, size);
    }
    /*获得索引I的元素*/
    public E get(int index){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("error");
        }
        Node cur = head.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.e;
    }
    /*得到首位元素*/
    public E getFirst(){
        return get(0);
    }
    /*得到末位元素*/
    public E getLast(){
        return get(getSize() - 1);
    }
    /*更新Index元素*/
    public void set(int index, E e){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("error");
        }
        Node cur = head.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }
    /*查找是否存在e*/
    public boolean contains(E e){
        Node cur = head.next;
        while (cur != null){
            if (cur.e.equals(e)){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    /*删除index*/
    public E delete(int index){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("error");
        }
        if (isEmpty()){
            throw new IllegalArgumentException("error");
        }
        Node pre = head;
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }
        Node deleteNode = pre.next;
        pre.next = deleteNode.next;
        deleteNode.next = null;
        size--;
        return deleteNode.e;
    }
    /*删除头部*/
    public E deleteFirst(){
        return delete(0);
    }
    /*删除尾部*/
    public E deleteLast(){
        return delete(getSize() - 1);
    }
    /*打印*/
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = head.next;
        while (cur != null){
            res.append(cur).append("->");
            cur = cur.next;
        }
        res.append("null");
        return res.toString();
    }
    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < 5; i++) {
            linkedList.addFirst(i);
            System.out.println(linkedList);
        }
        linkedList.add(6,3);
        System.out.println(linkedList);
        linkedList.delete(3);
        System.out.println(linkedList);
        linkedList.deleteFirst();
        System.out.println(linkedList);
        linkedList.deleteLast();
        System.out.println(linkedList);
        linkedList.deleteFirst();
        System.out.println(linkedList);
        linkedList.deleteFirst();
        System.out.println(linkedList);
        linkedList.deleteFirst();
        System.out.println(linkedList);

    }
}
